{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 05 准备配体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append(os.path.abspath('..'))\n",
    "from note_utils.path import chdir, add_rosetta_env\n",
    "\n",
    "rosetta_home=add_rosetta_env('~/git_develop/rosetta') # rosetta主目录路径自行修改\n",
    "tutorial_path=rosetta_home/'demos/tutorials/prepare_ligand'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- rosetta以残基为单位.不会单独处理原子.\n",
    "- 对于标准残基和部分小分子,rosetta知道键长,键角,以及残基的其他属性\n",
    "- rosetta在参数文件(params files)中编码残基的属性\n",
    "- rosetta数据库提供标准残基和部分小分子的参数\n",
    "- 对于rosetta不认识的配体,需要生成新的参数文件\n",
    "\n",
    "## 什么是参数文件\n",
    "\n",
    "- 参数文件是残基和配体提前计算好的几何信息和化学特征\n",
    "- 每种残基需要有对应的1个参数文件\n",
    "- `Rosetta/main/database/chemical/residue_type_sets/fa_standard/residue_types`中提供20种标准氨基酸,部分非标准氨基酸,水,金属,核酸等残基\n",
    "- 文件中每行的含义参见[残基参数文件]()\n",
    "\n",
    "## 下载和准备配体\n",
    "\n",
    "- 以获取血清素为例.有多种途径获取配体结构.例如访问[pdb高级搜索页面](https://www.rcsb.org/search/advanced),在`Chemical Attributes`字段中选取`chemical components`中的`Chemical Name`\n",
    "- 输入`serotonin`并提交查询;找到配体中的`SRO`;点击`Download Instance Coordinates`,选择SDF格式下载\n",
    "- 教程中包含下载好的配体文件,文件名为`serotonin.sdf`\n",
    "- 给配体添加H原子,有多种方法添加,本例使用openbabel.如需下载,使用`conda install -c conda-forge openbabel`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cd /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand/inputs\n",
      "当前目录从 /home/regen/git_develop/rosetta_notebook/00intro \n",
      "临时切换到 /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand/inputs\n",
      "27 molecules converted\n",
      "当前目录切换回到 /home/regen/git_develop/rosetta_notebook/00intro\n"
     ]
    }
   ],
   "source": [
    "with chdir(tutorial_path/'inputs'):\n",
    "    !obabel serotonin.sdf -O serotonin_withH.sdf -h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也可以使用avogadro等加氢.如果没有相关软件,输入目录里提前准备了加氢后的结构.\n",
    "\n",
    "## 生成参数文件\n",
    "\n",
    "rosetta提供脚本文件,以sdf作为输入文件,生成参数文件."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cd /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand\n",
      "当前目录从 /home/regen/git_develop/rosetta_notebook/00intro \n",
      "临时切换到 /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand\n",
      "Centering ligands at (  19.655,  -11.826,   -5.179)\n",
      "Atom names contain duplications -- renaming all atoms.\n",
      "WARNING:  structure contains double bonds but no aromatic bonds\n",
      "  Aromatic bonds must be identified explicitly --\n",
      "  alternating single/double bonds (Kekule structure) won't cut it.\n",
      "  This warning does not apply to you if your molecule really isn't aromatic.\n",
      "Total naive charge -0.460, desired charge 0.000, offsetting all atoms by 0.018\n",
      "WARNING: fragment 1 has 25 total atoms including H; protein residues have 7 - 24 (DNA: 33)\n",
      "Average 25.0 atoms (13.0 non-H atoms) per fragment\n",
      "(Proteins average 15.5 atoms (7.8 non-H atoms) per residue)\n",
      "WARNING:  no root atom specified, using NBR atom instead.\n",
      "Wrote params file SRO.params\n",
      "当前目录切换回到 /home/regen/git_develop/rosetta_notebook/00intro\n"
     ]
    }
   ],
   "source": [
    "with chdir(tutorial_path):\n",
    "    !$rosetta_home/source/scripts/python/public/molfile_to_params.py -n SRO -p SRO --conformers-in-one-file inputs/serotonin_withH.sdf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- -n选项是想要提供的配体名字(必须为3个字母)\n",
    "- -p选项给参数文件提供前缀名,将会生成`SRO.params`参数文件和`SRO_conformers.pdb`构象文件\n",
    "- molfile_to_params.py的更多选项可用--help选项查看\n",
    "\n",
    "生成的参数文件如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cd /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand\n",
      "当前目录从 /home/regen/git_develop/rosetta_notebook/00intro \n",
      "临时切换到 /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand\n",
      "NAME SRO\n",
      "IO_STRING SRO Z\n",
      "TYPE LIGAND\n",
      "AA UNK\n",
      "ATOM  C6  aroC  X   -0.10\n",
      "ATOM  C5  aroC  X   -0.10\n",
      "ATOM  N1  Ntrp  X   -0.59\n",
      "ATOM  C4  aroC  X   -0.10\n",
      "...\n",
      "BOND_TYPE  O1   C1  1   \n",
      "BOND_TYPE  O1   H1  1   \n",
      "BOND_TYPE  C1   C8  2   \n",
      "BOND_TYPE  C1   C2  1   \n",
      "...\n",
      "CHI 1  C2   C1   O1   H1 \n",
      "PROTON_CHI 1 SAMPLES 2 0 180 EXTRA 1 20\n",
      "CHI 2  C5   C6   C9   C10\n",
      "CHI 3  C6   C9   C10  N2 \n",
      "...\n",
      "NBR_ATOM  C6 \n",
      "NBR_RADIUS 5.878178\n",
      "...\n",
      "ICOOR_INTERNAL    C6     0.000000    0.000000    0.000000   C6    C5    N1 \n",
      "ICOOR_INTERNAL    C5     0.000000  180.000000    1.388444   C6    C5    N1 \n",
      "ICOOR_INTERNAL    N1     0.000000   70.459479    1.353928   C5    C6    N1 \n",
      "ICOOR_INTERNAL    C4     0.063216   71.134980    1.346685   N1    C5    C6 \n",
      "...\n",
      "PDB_ROTAMERS SRO_conformers.pdb\n",
      "当前目录切换回到 /home/regen/git_develop/rosetta_notebook/00intro\n"
     ]
    }
   ],
   "source": [
    "with chdir(tutorial_path):\n",
    "    !head -n 8 SRO.params\n",
    "    !echo ...\n",
    "    !grep -G '^BOND' -m 4 SRO.params\n",
    "    !echo ...\n",
    "    !grep -G 'CHI' -m 4 SRO.params\n",
    "    !echo ...\n",
    "    !grep -G '^NBR' -m 4 SRO.params\n",
    "    !echo ...\n",
    "    !grep -G '^ICOOR' -m 4 SRO.params\n",
    "    !echo ...\n",
    "    !tail -n 1 SRO.params"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 文档开头是配体的基础信息.rosetta将其识别为LIGAND类别,名字为SRO,单字母残基名为Z,三字母残基名为UNK\n",
    "- 然后是ATOM开头的原子信息,从左到右分别是原子名,原子类型,\"X\",部分电荷\n",
    "- BOND_TYPE开头的行定义共价键信息,包括成键原子名及价键\n",
    "- CHI定义旋转chi角,从左到右分别是chi角编号,相关的4个原子.`PROTON_CHI`表示该CHI中的质子需要被采样多少次\n",
    "- NBR_ATOM表示rosetta中的邻位原子.对于配体,默认表示的是距离质心最近的原子\n",
    "- NBR_RADIUS表示配体的大小.表示邻位原子与其他重原子中最远的距离\n",
    "- ICOOR_INTERNAL定义原子的内部坐标\n",
    "- PDB_ROTAMERS指定构象pdb文件的路径\n",
    "\n",
    "## 在运行过程中使用参数文件\n",
    "\n",
    "需要使用非标准残基的应用,使用`-extra_res_fa <path-to-params-file>`指定参数文件路径.\n",
    "\n",
    "## 更多进阶准备\n",
    "\n",
    "通常,你需要为配体提供多个构象.可以用openbabel或avogadro生成.\n",
    "\n",
    "例如openbabel使用[--confab或者--conformer](https://open-babel.readthedocs.io/en/latest/3DStructureGen/multipleconformers.html)生成多样性的构象.\n",
    "\n",
    "## 其他残基类型\n",
    "\n",
    "rosetta支持D型氨基酸,非标氨基酸,RNA,金属离子等.参见[非标准残基]()."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cd /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand\n",
      "当前目录从 /home/regen/git_develop/rosetta_notebook/00intro \n",
      "临时切换到 /home/regen/git_develop/rosetta/demos/tutorials/prepare_ligand\n",
      "当前目录切换回到 /home/regen/git_develop/rosetta_notebook/00intro\n"
     ]
    }
   ],
   "source": [
    "with chdir(tutorial_path):\n",
    "    !rm SRO*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
